// RunGame.cpp : implementation of the CRunGame class
//
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "resource.h"
#include "RunGame.h"
#include "RakPeerInterface.h"
#include "util.h"

LRESULT CRunGame::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
{
	m_wndListPlayers = GetDlgItem(IDC_PLIST);
	m_wndListPlayers.SetExtendedListViewStyle(LVS_EX_FULLROWSELECT);
	m_wndListPlayers.InsertColumn(0, _T("Name"), LVCFMT_LEFT , 100, 0);
	m_wndListPlayers.InsertColumn(1, _T("IP"), LVCFMT_LEFT , 100, 0);
	m_wndListPlayers.InsertColumn(2, _T("Ready"), LVCFMT_LEFT , 100, 0);
	CenterWindow(GetParent());
	FillPlayersList();

	return TRUE;
}

LRESULT CRunGame::OnOK(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	EndDialog(wID);
	return 0;
}

LRESULT CRunGame::OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	EndDialog(wID);
	return 0;
}

LRESULT CRunGame::OnUpdatePlayers(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	FillPlayersList();
	return 0;
}

BOOL CRunGame::FillPlayersList()
{
	m_wndListPlayers.DeleteAllItems();
	if (server)
	{
		RakNet::SystemAddress systems[10];
		unsigned short numConnections=10;
		server->GetConnectionList((RakNet::SystemAddress*) &systems, &numConnections);

		for (int i = 0; i < numConnections; i++)
		{
			CString connection(ParseParameter(systems[i].ToString(true))[0].c_str());
			m_wndListPlayers.AddItem(i,0,_T("your name?"));
			m_wndListPlayers.AddItem(i,1,connection);
		}
	}
	else if (client)
	{
	
	}
	return TRUE;

}
LRESULT CRunGame::OnReady(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	readyEventPlugin.SetEvent(0,true);
	return 0;
}
